package com.ycl2.imports.esconfig;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.ycl2.imports.controller.ImportexcelController;
import com.ycl2.imports.entity.Importexcel;
import com.ycl2.imports.lisner.EasyExcelImport;
import com.ycl2.imports.service.impl.ImportexcelServiceImpl;
import com.ycl2.imports.task.MyTask02;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者: LCG
 * 日期: 2020/7/28 09:03
 * 描述:
 */
@Configuration
public class EsConfig {

    @Autowired
    private EsZkConfig esZkConfig;


    @Autowired
    private ImportexcelController myTask02;

    @Autowired
    private ImportexcelServiceImpl importexcelService;





    @Bean("queue01")
    public Queue queue1(){
        //参数：1、队列的名字  2是否持久化 4是否是独占队列  4、当没有链接通道的时候时候自动删除
        Queue queue = new Queue("Que",true,false,false);
        return queue;
    }

    @Bean("exchange1")
    public Exchange exchange1(){
        Exchange build = ExchangeBuilder.directExchange("T-Exchange1").durable(true).build();
        return build;
    }

    @Bean("bangding1")
    public Binding binding1(Queue queue01, Exchange exchange1){
        Binding keyy = BindingBuilder.bind(queue01).to(exchange1).with("").noargs();
        return keyy;
    }

/*    @Bean("import")
    public void mains() {
        // 写法2：
        String fileName = "I:\\getExec\\遥_0.xlsx";
        ExcelReader excelReader = EasyExcel.read(fileName, Importexcel.class, new EasyExcelImport(importexcelService)).build();
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        excelReader.read(readSheet);
        // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
        excelReader.finish();
    }*/






 /*   @Autowired
    private MyTask02 myTask02;*/

    /**
     * Zookeeper的配置
     * @param esZkConfig
     * @return
     */
    @Bean("zookeeperConfiguration")
    public ZookeeperConfiguration zookeeperConfiguration(EsZkConfig esZkConfig){
        System.out.println("====================1");
        ZookeeperConfiguration zookeeperConfiguration=new ZookeeperConfiguration(esZkConfig.getList(),esZkConfig.getNamespace());
        return zookeeperConfiguration;
    }


    /**
     * 配置Zookeeper注册中心
     * @param zookeeperConfiguration
     * @return
     */
    @Bean(initMethod = "init",value = "zookeeperRegistryCenter")
    public ZookeeperRegistryCenter zookeeperRegistryCenter(ZookeeperConfiguration zookeeperConfiguration){
        System.out.println("====================2");
        ZookeeperRegistryCenter zookeeperRegistryCenter=new ZookeeperRegistryCenter(zookeeperConfiguration);
        return zookeeperRegistryCenter;
    }


    /**
     * job的核心配置
     * @return
     */
   @Bean("jobCoreConfiguration")
   public JobCoreConfiguration jobCoreConfiguration(){
       System.out.println("====================3");
       JobCoreConfiguration simpleCoreConfig =
               JobCoreConfiguration.newBuilder("mytask02", "0/120 * * * * ?", 4)
                       .description("描述任务的信息")
                       .failover(true)
                       .jobParameter("可以为任务传入一些固定的参数")
                       .shardingItemParameters("0=T00,1=T01,2=T02,3=T03")
                       .build();

       return simpleCoreConfig;

   }




    /**
     * 配置任务
     * @param jobCoreConfiguration
     * @return
     */
   @Bean("simpleJobConfiguration")
   public SimpleJobConfiguration simpleJobConfiguration(JobCoreConfiguration jobCoreConfiguration){
       System.out.println("====================4");
       SimpleJobConfiguration simpleJobConfiguration=new SimpleJobConfiguration(jobCoreConfiguration, MyTask02.class.getCanonicalName());
       return simpleJobConfiguration;
   }

   @Bean("liteJobConfiguration")
   public LiteJobConfiguration liteJobConfiguration(SimpleJobConfiguration simpleJobConfiguration){
         // 定义Lite作业根配置
       LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfiguration).build();
       return simpleJobRootConfig;
   }

    /**
     * 配置并初始化调度任务
     * @param zookeeperRegistryCenter
     * @param liteJobConfiguration
     * @return
     */
   @Bean(value = "jobScheduler",initMethod = "init")
   public JobScheduler jobScheduler(ZookeeperRegistryCenter zookeeperRegistryCenter, LiteJobConfiguration liteJobConfiguration){
      // JobScheduler jobScheduler=new JobScheduler(zookeeperRegistryCenter,liteJobConfiguration);
       System.out.println("====================5");
       SpringJobScheduler springJobScheduler=new SpringJobScheduler(myTask02,zookeeperRegistryCenter,liteJobConfiguration);

       return springJobScheduler;
   }

}
